//
//  GPConversationItem.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/24.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPConversationItem.h"

@implementation GPConversationItem

- (instancetype)initWithConversation:(EMConversation *)conversation
{
    self = [super init];
    if (self) {
        _conversation = conversation;
        
        
        EMMessage *message = conversation.latestMessage;
        EMMessageBody *body = conversation.latestMessage.body;
        if (body.type == EMMessageBodyTypeText) {
            EMTextMessageBody *textBody = (EMTextMessageBody *)body;
            _lastMsgContent = textBody.text;
        }else if (body.type == EMMessageBodyTypeImage){
            _lastMsgContent = @"[Image]";
            //判断是否是SOS紧急消息
            NSDictionary *ext = message.ext;
            if (ext == nil || ![ext.allKeys containsObject:@"latitude"] || ![ext.allKeys containsObject:@"longitude"] ) {
                
            }else{
                _lastMsgContent = @"[SOS Message]";
            }
            
        }else if (body.type == EMMessageBodyTypeVideo){
            _lastMsgContent = @"[Video Call]";
        }else if (body.type == EMMessageBodyTypeVoice){
            _lastMsgContent = @"[Voice Call]";
        }else if (body.type == EMMessageBodyTypeLocation) {
            _lastMsgContent = @"[Location]";
            EMLocationMessageBody *locationBody = (EMLocationMessageBody *)body;
            if ([locationBody.address isEqualToString:@"SOS test"]) {
                _lastMsgContent = @"[SOS Message]";
            }
        }
        else{
            _lastMsgContent = @"[Others]";
        }
        
        CGFloat timestamp = conversation.latestMessage.localTime  / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
        
        _lastMsgDisplayTime = [date timeIntervalDescription];
        
        _unreadCount = [conversation unreadMessagesCount];
        
        
    }
    
    return self;
}

@end
